
function Picture(obj){
	this.url = obj.url;
}


angular.module('picturesEditor').controller('PicturesEditorCtrl', function($scope, $http) {
	$scope.pictures = [
	];

	$scope.init = function(form, uploadUrl, baseUrl, picturesJson){
		$scope.formId = form;
		$scope.uploadUrl = uploadUrl;
		$scope.baseUrl = baseUrl;
		$scope.pictures = angular.fromJson(picturesJson);
	};
	$scope.deletePicture = function(picture){
		$scope.pictures.splice($scope.pictures.indexOf(picture), 1);
	};
	$scope.addPicture = function(picture){
		$scope.$apply(function () {
			$scope.pictures.push(new Picture(picture));
		});
	};

	$scope.uploadPicture = function(){
		var formData = new FormData($('#' + $scope.formId)[0]);
		$.ajax({
			url: $scope.uploadUrl,
			type: 'POST',
			xhr: function() {
				var myXhr = $.ajaxSettings.xhr();
				return myXhr;
			},
			success: function (data) {
				$scope.addPicture({'url': $scope.baseUrl + data});
			},
			data: formData,
			cache: false,
			contentType: false,
			processData: false
		});
	};
});